package com.yx.frame.builder;

import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;

import com.yx.frame.builder.code.CodeGenerator;


import org.eclipse.swt.widgets.Label;

public class CodeBuildDialog extends Dialog {

	protected Object result;
	protected Shell shell;
	private Text message;
	Button close;
	ProgressBar progressBar;
	Button IS_JDK18;
	Button IS_JETY;
	private Text jsFileName;
	private Text jsControlName;
	private Label lblUid;
	private Text TimeDif;

	/**
	 * Create the dialog.
	 * 
	 * @param parent
	 * @param style
	 */
	public CodeBuildDialog(Shell parent, int style) {
		super(parent, style);
		setText("代码生成神器");
	}

	/**
	 * Open the dialog.
	 * 
	 * @return the result
	 */
	public Object open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shell = new Shell(getParent(), SWT.BORDER | SWT.TITLE
				| SWT.PRIMARY_MODAL);
		shell.setBackground(SWTResourceManager
				.getColor(SWT.COLOR_WIDGET_LIGHT_SHADOW));
		shell.setSize(623, 345);
		shell.setText(getText());

		Button btnNewButton = new Button(shell, SWT.NONE);
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				Display.getDefault().syncExec(new Runnable() {
	                public void run() {
	                	close.setEnabled(false);
	                	String jsFile=jsFileName.getText().trim();
	                	String jsControl=jsControlName.getText().trim();
	                	boolean isJDK18=true;
	                	boolean isJety=true;
	                	if(!IS_JDK18.getSelection()){
	                		isJDK18=false;
	                	}
	                	if(!IS_JETY.getSelection()){
	                		isJety=false;
	                	}
	                	long timeDifference=-1;
	                	try{
	                		timeDifference=Long.parseLong(TimeDif.getText().trim())*1000;
	                		if(timeDifference<0)timeDifference=-1;
	                	}catch(Exception e){}
	                	if(!"".equals(jsFile)&&!"".equals(jsControl)){
	                		CodeGenerator CodeGenerator = new CodeGenerator(
	                				LeftControlTree.ROOT_PATH,jsFile,jsControl, message, progressBar,isJDK18,isJety,timeDifference);
	                	}	                	
	    				close.setEnabled(true);
	               }
				}); 				
			}
		});
		btnNewButton.setBounds(527, 5, 80, 27);
		btnNewButton.setText("开始编译");

		progressBar = new ProgressBar(shell, SWT.NONE);
		progressBar.setBounds(10, 34, 597, 17);

		message = new Text(shell, SWT.BORDER | SWT.WRAP | SWT.H_SCROLL | SWT.CANCEL);
		message.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		message.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
		message.setEditable(false);
		message.setBounds(10, 59, 597, 212);

		close = new Button(shell, SWT.NONE);
		close.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				shell.close();
				shell.dispose();
			}
		});
		close.setBounds(527, 277, 80, 27);
		close.setText("关闭");
		
		Label lbljs = new Label(shell, SWT.NONE);
		lbljs.setBounds(10, 10, 66, 17);
		lbljs.setText("JS文件名：");
		
		Label lblJs = new Label(shell, SWT.NONE);
		lblJs.setBounds(287, 10, 109, 17);
		lblJs.setText("JS对象名（大写）：");
		
		jsFileName = new Text(shell, SWT.BORDER);
		jsFileName.setText("smartframe");
		jsFileName.setBounds(82, 7, 185, 23);
		
		jsControlName = new Text(shell, SWT.BORDER);
		jsControlName.setText("SFC");
		jsControlName.setBounds(402, 7, 98, 23);
		
		IS_JDK18 = new Button(shell, SWT.CHECK);
		IS_JDK18.setSelection(true);
		IS_JDK18.setBounds(10, 282, 60, 17);
		IS_JDK18.setText("JDK1.8");
		
		IS_JETY = new Button(shell, SWT.CHECK);
		IS_JETY.setSelection(true);
		IS_JETY.setBounds(72, 282, 98, 17);
		IS_JETY.setText("使用内嵌JETY");
		
		lblUid = new Label(shell, SWT.NONE);
		lblUid.setText("UID有效期：");
		lblUid.setBounds(177, 282, 72, 17);
		
		TimeDif = new Text(shell, SWT.BORDER);
		TimeDif.setText("864000");
		TimeDif.setBounds(251, 279, 80, 23);
		
		Label label_1 = new Label(shell, SWT.NONE);
		label_1.setBounds(334, 282, 22, 17);
		label_1.setText("秒");

	}
}
